<?php
declare (strict_types=1);

namespace app\admin\validate;

use think\Validate;

class ModuleField extends Validate
{
    protected $rule = [
        'name'       => 'require',
        'code'       => 'require|checkExistCode',
        'field_type' => 'require',
        'status'     => ['require', 'in' => '1,2'],
    ];

    protected $message = [
        'name.require'       => '请输入字段名称',
        'code.require'       => '请输入字段标识',
        'field_type.require' => '请输入字段类型',
        'status'             => '请选择正确的状态',
    ];

    protected $scene = [
        'add'  => ['name', 'code', 'status'],
        'edit' => ['name', 'status']
    ];


    public function checkExistCode($value, $rule, $data = [])
    {
        $moduleModel = new \app\common\model\ModuleField([], $data['website_code']);
        $module      = $moduleModel->findModuleFieldByCode($data['module_id'], $value);
        if (!empty($module)) {
            return '字段标识已存在';
        }

        return true;
    }
}
